#### Import Data ####

fig4_dat = bind_rows(import(here("data","fig4_inc_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig4_inc_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |> 
  invert_group()

fig6_dat = bind_rows(import(here("data","fig6_edu_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig6_edu_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |>
  invert_group()

#### Calculate Differences ####

inc_agree = left_join(fig4_dat %>% filter(group==1 & agree == "Agree"),
                      fig4_dat %>% filter(group==0 & agree == "Agree"),
                      by="condition") |> 
  mutate(inc_agree_diff = round(effect.x - effect.y,2),
         inc_agree_ci_lo = round(-1*ci_int_high.x,2), # -1 x since interaction was calculated w/ opposite groupings
         inc_agree_ci_hi = round(-1*ci_int_low.x,2),
         inc_agree = paste0(inc_agree_diff,"\n(",
                            inc_agree_ci_lo,", ",inc_agree_ci_hi,")") |> 
           linebreak()
         ) |> 
  select(condition, inc_agree_diff, inc_agree)

inc_disagree = left_join(fig4_dat %>% filter(group==1 & agree == "Disagree"),
                      fig4_dat %>% filter(group==0 & agree == "Disagree"),
                      by="condition") |> 
  mutate(inc_disagree_diff = round(effect.x - effect.y,2),
         inc_disagree_ci_lo = round(-1*ci_int_high.x,2),
         inc_disagree_ci_hi = round(-1*ci_int_low.x,2),
         inc_disagree = paste0(inc_disagree_diff,"\n(",
                            inc_disagree_ci_lo,", ",inc_disagree_ci_hi,")") |> 
           linebreak()) |> 
  select(condition, inc_disagree_diff, inc_disagree)

edu_agree = left_join(fig6_dat %>% filter(group==1 & agree == "Agree"),
                      fig6_dat %>% filter(group==0 & agree == "Agree"),
                      by="condition") |> 
  mutate(edu_agree_diff = round(effect.x - effect.y,2),
         edu_agree_ci_lo = round(-1*ci_int_high.x,2),
         edu_agree_ci_hi = round(-1*ci_int_low.x,2),
         edu_agree = paste0(edu_agree_diff,"\n(",
                            edu_agree_ci_lo,", ",edu_agree_ci_hi,")") |> 
           linebreak()) |> 
  select(condition, edu_agree_diff, edu_agree)

edu_disagree = left_join(fig6_dat %>% filter(group==1 & agree == "Disagree"),
                      fig6_dat %>% filter(group==0 & agree == "Disagree"),
                      by="condition") |> 
  mutate(edu_disagree_diff = round(effect.x - effect.y,2),
         edu_disagree_ci_lo = round(-1*ci_int_high.x,2),
         edu_disagree_ci_hi = round(-1*ci_int_low.x,2),
         edu_disagree = paste0(edu_disagree_diff,"\n(",
                            edu_disagree_ci_lo,", ",edu_disagree_ci_hi,")") |> 
           linebreak()) |> 
  select(condition, edu_disagree_diff, edu_disagree)

econ = c("gov_insurance", "public_option", "health_subsidies",
         "mctaxes", "estate_tax", "uctaxes", "college",
         "trade", "minwage", "gauranteed_jobs", "right_to_work",
         "medicare_for_all", "uctaxes2", "abolish_priv_insurance",
         "china_tariffs", "forgive_debt", "wealth_tax","housing_guarantee")

#### Generate Table ####

tab_df = inc_agree |> 
  left_join(inc_disagree) |>
  left_join(edu_agree) |> 
  left_join(edu_disagree) |> 
  filter(condition %in% econ) |> 
  left_join(policies |> 
              select(label, newlabel),
            join_by(condition == label)) |> 
  arrange(newlabel) |> 
  select(newlabel, inc_agree, inc_disagree, edu_agree, edu_disagree) |> 
  kable(col.names = c("Policy","Agree","Disagree",
                      "Agree","Disagree"),
        format = 'latex', booktabs = T) |> 
  kable_classic(full_width = F) |> 
  add_header_above(c(" " = 1, "Income" = 3, "Education" = 3))

tab_df |> 
  fix_table() |> 
  writeLines(here("tables","tab_2.tex"))

ccat("See 'tab_2.tex' for Table 2 output")
